package db

import (
	"math"
)

type Param interface {
	string | struct{} |
	CreateUserParam | CreateRoleParam | CreateUserBelongsToRoleParam
}

var NoArg = struct{}{}

type Pager struct {
	PageSize uint
	PageNo   uint
}

type ParamConverter[T Param] struct {
	arg   *T
	pager *Pager
}




func NewParamConverter[T Param](arg *T) *ParamConverter[T] {
	return &ParamConverter[T]{
		arg:   arg,
		pager: nil,
	}
}

func (c *ParamConverter[T]) WithPager(pager *Pager) *ParamConverter[T] {
	c.pager = pager
	return c
}

func (c *ParamConverter[T]) AsMap() (map[string]any, error) {
	r := make(map[string]any)
	var err error
	switch any(*c.arg).(type) {
	case string:
		r["uuid"] = *c.arg
	case struct{}:
	default:
		r, err = StructToMap(c.arg, "map", "StructToMap")
	}
	if c.pager != nil {
		r["skip"] = uint(math.Max(float64(c.pager.PageNo)-1, 0))
		r["limit"] = c.pager.PageSize
	}
	return r, err

}
